'玩家
目前有小bug，当多个敌人碰撞玩家时，无敌计数器结束后，受到的伤害为最后进入玩家碰撞的敌人的伤害
'
extends CharacterBody2D

const  SPEED = 100#初始速度
const ACCELERATION = 15#初始加速度

signal died#玩家死亡信号

@onready var damage_interval_timer = $DamageIntervalTimer
@onready var health_component = $HealthComponent
@onready var health_bar = $HealthBar
@onready var ability_manager = $AbilityManager
@onready var animation = $AnimationPlayer

var MOVEMODE = 1#1为键盘移动，2为鼠标跟随
var can_move:bool = true#鼠标控制下的行走和移动
var direction = Vector2.ZERO#方向
var number_colliding_bodies = 0#目前碰撞玩家的数量
var hurt_damage = 0#正受到的伤害
var speed = 0#玩家速度

func _ready() -> void:
	MOVEMODE = GameSaves.get_option_data("move_mode")#设定移动方式
	speed = SPEED#设置速度
	$collisionArea2D.body_entered.connect(on_body_entered)#敌人进入玩家碰撞范围
	$collisionArea2D.body_exited.connect(on_body_exited)#敌人离开玩家碰撞范围
	health_component.died.connect(on_died)#死亡信号
	damage_interval_timer.timeout.connect(on_damage_interval_timer)#伤害间隔计时器
	health_component.health_change.connect(on_health_change)#血量改变计时器
	updated_health_display()#更新血量显示
	pass


func _process(delta: float) -> void:
	if MOVEMODE == 1:
		direction =  get_movement_vector().normalized()
	else:
		if can_move:
			direction = get_movement_vector_bymouse().normalized()
		else:
			direction = Vector2.ZERO
	self.velocity = self.velocity.lerp(direction * speed,1 - exp(-delta * ACCELERATION))
	self.move_and_slide()
	#行走动画
	if direction.x || direction.y:
		animation.play("walk")
	else:
		animation.play("RESET")
	#精灵朝向
	var move_sign = signf(direction.x)
	if move_sign < 0:
		$Sprite2D.flip_h = true
	else:
		$Sprite2D.flip_h = false
	pass

#鼠标移动模式下，左键停止移动
func _input(event: InputEvent) -> void:
	if MOVEMODE == 1:
		return
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.is_pressed():
			can_move = false
		else:
			can_move = true
	pass

#通过键盘获得方向
func get_movement_vector() -> Vector2:
	var x_movement = Input.get_action_strength("move_rigth") - Input.get_action_strength("move_left")
	var y_movement = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")  
	return Vector2(x_movement,y_movement)

#通过鼠标获得方向
func get_movement_vector_bymouse() -> Vector2:
	var x_movement = get_global_mouse_position().x - self.global_position.x
	var y_movement = get_global_mouse_position().y - self.global_position.y
	return Vector2(x_movement,y_movement)

#判断当前是否能造成伤害，如果可以则造成伤害
func check_deal_damage(damage: int) -> void:
	if !number_colliding_bodies || !damage_interval_timer.is_stopped():
		return
	GameEvents.emit_player_damage()
	$RanAudComponent.play_random()
	health_component.damage(damage)
	damage_interval_timer.start()
	pass

#更新血条显示
func updated_health_display() -> void:
	health_bar.value = health_component.get_health_percent()
	pass

#玩家被碰撞时，或者怪物进入玩家碰撞时
func on_body_entered(body: Node2D) -> void:
	if !body.is_in_group("enemy"):
		return
	number_colliding_bodies += 1
	hurt_damage = body.damage
	check_deal_damage(hurt_damage)
	pass

#碰撞离开玩家时，或者怪物离开玩家碰撞时
func on_body_exited(body: Node2D) -> void:
	if !body.is_in_group("enemy"):
		return
	number_colliding_bodies -= 1
	pass

#计时结束，判断当前是否能造成伤害
func on_damage_interval_timer() -> void:
	check_deal_damage(hurt_damage)
	pass

#血量改变时更新血条显示
func on_health_change() -> void:
	updated_health_display()
	pass

#玩家死亡
func on_died() -> void:
	died.emit()
	self.queue_free.call_deferred()
	pass
